package cn.yjr.vector;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRApplication;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class WorkSettingActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	Context context;
	String response;
	JSONObject jsonInfo;
	String phone, pwd;
	LinearLayout work_period_ll;
	RelativeLayout zxjg_rl, jhyzf_rl, hfsx_rl;
	TextView zxjg_tv, jhyzf_tv, hfsx_tv;
	Button add_time;
	JSONObject userData;
	String workPeriod;
	static final int LOGIN_MSG = 0;
	SharedPreferencesHelper spHelper;
	static String login_url = "http://115.28.222.1/yijiaren/user/login.action";
	String[] weeks = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
	String[] days = {"上午","下午"};
	String[] types = {"普通门诊","专家门诊","特需门诊"};
	String[] a = {"P","Z","T"};
	String[] b = {"A","B"};
	String zxjg, jhyzf, hfsx;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_work_setting);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		Intent intent = this.getIntent();
		try {
			userData = new JSONObject(intent.getStringExtra("userData"));
			workPeriod = userData.getString("workPeriod");
			zxjg = userData.getString("consultPrice");
			jhyzf = userData.getString("plusPrice");
			hfsx = userData.getString("maxResponseGap");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.spHelper = new SharedPreferencesHelper(this.context);
		initTitleBar();
		
		initComponent();
		
		initEventListener();
		
		
		
	
	}
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.work_setting_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		work_period_ll = (LinearLayout) findViewById(R.id.work_period_ll);
		add_time = (Button) findViewById(R.id.add_time);
		
		zxjg_rl = (RelativeLayout) findViewById(R.id.zxjg_rl);
		jhyzf_rl = (RelativeLayout) findViewById(R.id.jhyzf_rl);
		hfsx_rl = (RelativeLayout) findViewById(R.id.hfsx_rl);
		
		zxjg_tv = (TextView) findViewById(R.id.zxjg_tv);
		jhyzf_tv = (TextView) findViewById(R.id.jhyzf_tv);
		hfsx_tv = (TextView) findViewById(R.id.hfsx_tv);
		
		String week,day,type;
		//这里可能没有设置出诊时间，wordperiod不是空串，而是一个null可能
		//TODO
		for(int i = 0; i < workPeriod.length(); i+=4){
			char w = workPeriod.charAt(i+1);
			char d = workPeriod.charAt(i+2);
			char t = workPeriod.charAt(i+3);
			week = weeks[Integer.parseInt(w +"")-1];
			if(d=='A'){
				day = days[0];
			}else{
				day = days[1];
			}
			if(t=='P')
			{
				type = types[0];
			}else if(t=='Z'){
				type = types[1];
			}else{
				type = types[2];
			}
			addWorkPeriod(week,day,type);
		}
		
		if(zxjg==null||zxjg.equals("")||zxjg.equals("0")){
			zxjg_tv.setText("未设定");
		}else{
			zxjg_tv.setText(zxjg + "元/次");
		}
		if(jhyzf==null||jhyzf.equals("")||jhyzf.equals("0")){
			jhyzf_tv.setText("未设定");
		}else{
			jhyzf_tv.setText(jhyzf+ "元/次");
		}
		hfsx_tv.setText(hfsx+ "小时");
		
	}
	
	private void initEventListener(){
		
		add_time.setOnClickListener(this);
		zxjg_rl.setOnClickListener(this);
		jhyzf_rl.setOnClickListener(this);
		hfsx_rl.setOnClickListener(this);
		
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId()){
		case R.id.add_time:
			intent = new Intent(WorkSettingActivity.this, WorkPeriodActivity.class);
			startActivityForResult(intent, 0);
			break;
		case R.id.zxjg_rl:
			intent = new Intent(WorkSettingActivity.this, ConsultPriceActivity.class);
			startActivityForResult(intent,1);
			break;
		case R.id.jhyzf_rl:
			intent = new Intent(WorkSettingActivity.this, PlusPriceActivity.class);
			startActivityForResult(intent,2);
			break;
		case R.id.hfsx_rl:
			intent = new Intent(WorkSettingActivity.this, ResponseTimeActivity.class);
			startActivityForResult(intent,3);
			break;
		default:
				break;
		}
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
	      switch (requestCode) {
	      case 0:  
	    	  if(data!=null){
	    		  String week = data.getStringExtra("week");
	    		  String day= data.getStringExtra("day");
	    		  String type = data.getStringExtra("type");
	    		  addWorkPeriod(week,day,type);
	    	  }
	    	  break;
	      case 1:
	    	  if(data!=null){
	    		  String price = data.getStringExtra("price");
	    		  zxjg_tv.setText(price+ "元/次");
	    		  
	    	  }
	    	  break;
	      case 2:
	    	  if(data!=null){
	    		  String price = data.getStringExtra("price");
	    		  jhyzf_tv.setText(price+ "元/次");
	    	  }
	    	  break;
	      case 3:
	    	  if(data!=null){
	    		  String time = data.getStringExtra("time");
	    		  hfsx_tv.setText(time+ "小时");
	    	  }
	    	  break;
	      default:  
	          break;  

	      }  
	      super.onActivityResult(requestCode, resultCode, data);  
	  }  

	void addWorkPeriod(String week, String day, String type){
		View tmp = inflater.inflate(R.layout.work_period_item, null);
		TextView work_period_time = (TextView) tmp.findViewById(R.id.work_period_time);
		TextView work_period_type = (TextView) tmp.findViewById(R.id.work_period_type);
		work_period_time.setText(week + " " + day);
		work_period_type.setText(type);
		work_period_ll.addView(tmp);
	}
}